package com.springboot.eduservice.controller;


import com.springboot.commonutils.R;
import com.springboot.eduservice.Client.VodClient;
import com.springboot.eduservice.entity.EduVideo;
import com.springboot.eduservice.service.impl.EduVideoServiceImpl;
import com.springboot.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-12
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoServiceImpl eduVideoService;

    @Autowired
    private VodClient client;

    @GetMapping("selectVideoById/{VideoId}")
    public R selectVideoById(@PathVariable String VideoId){
        EduVideo video = eduVideoService.getById(VideoId);
        return R.ok().data("video",video);
    }

    @PostMapping("addVideoInfo")
    public R addVideoInfo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    @DeleteMapping("deleteVideo/{VideoId}")
    public R deleteVideo(@PathVariable String VideoId){
        EduVideo id = eduVideoService.getById(VideoId);
        String videoSourceId = id.getVideoSourceId();
        System.out.println(videoSourceId+"++++++++++++++++++++++++++");
        if (!StringUtils.isEmpty(videoSourceId)){
            R result = client.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001){
                throw new GuliException(20001,"视频删除失败，熔断器....");
            }
        }
        //删除小节
        eduVideoService.removeById(VideoId);
        return R.ok();
    }

    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

}

